package ControlLayer;
import ModelLayer.*;

import java.util.Scanner;
/**
 * Sale Controller:
 * Connects the SaleUI and newSaleUI to the model layer.
 * 
 * @author Gruppe 3
 * @version 1.0
 */
public class SaleCtr
{   
    // Controllers    
    private PersonCtr personCtr;
    private StorageCtr storageCtr;
    private SaleCtr instance;
    
    // container
    private SaleContainer saleContainer;
    private Sale newSale;   
    
    /**
     * constructor
     */
    public SaleCtr()
    {   
       saleContainer = SaleContainer.getInstance();
       this.personCtr = new PersonCtr();
       this.storageCtr = new StorageCtr(); 
       newSale = Sale.getInstance();
    }
    
    public static SaleContainer getInstance()
    {
        if(instance == null)
        {
            instance = new SaleContainer();
        }
        return instance;
    }
    
    /**
     * Checks if a string is a double and returns a boolean based on this
     */
        private boolean isDouble(String checkInput)
    {
        try
        {
            Double.parseDouble(checkInput);
        }
        catch(NumberFormatException e)
        {   
            System.out.println("Du skal skrive et heltal eller decimaltal.");
            return false;
        }
        
        return true;        
    } 
    
    /**
     * input of a double
     */
    private double inputDouble()
    {
        Scanner keyboard = new Scanner(System.in);
        double newInput = keyboard.nextDouble();
        return newInput;
    }
    
    /**
     * adds an item to the sale newSale
     * 
     * @param searchItemcode  item to find and add
     * @param amount          amount of item to add
     */
    public void addItem(String searchItemCode, int amount)
    {   
        Item newSaleItem = storageCtr.getItem(searchItemCode);
        
        if (newSale != null && newSaleItem != null)
        {               
            newSale.addSaleItem(newSaleItem, amount);
        }
    }
    
    /**
     * removes an item from the sale newSale
     * @param searchItemCode  item to remove from newSale
     */
    public void removeItem(String searchItemCode)
    {
        if (newSale != null && searchItemCode != null)
        {
            newSale.removeSaleItem(searchItemCode);
        }
    }
    
    /**
     * adds an employee to the sale newSale
     * @param employeeID  id of the employee to add to the sale newSale
     */
    public void addEmployee(String employeeID)
    {
        Employee saleEmployee = personCtr.getEmployee(employeeID);
        
        if (newSale != null && saleEmployee != null)
        {   
            newSale.setEmployee(saleEmployee);
            System.out.println("Ansattes navn: " + newSale.getEmployee().getName());
        }
    }
    
     /**
     * adds a customer to the sale newSale 
     * @param customerID  id of the customer to add to the sale newSale
     */
    public void addCustomer(String customerID)
    {
        Customer saleCustomer = personCtr.getCustomer(customerID);
        
        if (newSale != null && saleCustomer != null)
        {
            newSale.setCustomer(saleCustomer);
        }
    }
    
    /**
     * lists all the partsales (the items with their amount) in the sale newSale
     */
    public void listAllPartSales()
    {   
        if (newSale != null)
        {
            if (newSale.getEmployee() != null)
            {
                System.out.println("Ansat: " + newSale.getEmployee().getName());
            }
        
            if (newSale.getCustomer() != null)
            {
                System.out.println("Kunde: " + newSale.getCustomer().getName());
            }
        
            if (newSale != null)
            {
                newSale.listAllPartSales();            
            }
        }
    }
    
    /**
     * ends the sale
     */
    public void endSale()
    {   
        // checks if an employee is assigned to the sale
        if (newSale != null && newSale.getEmployee() != null)
        {   
            boolean checkPayment = false;
            double totalSalePrice = newSale.endSale();
            double totalSalePriceWithDiscount = 0.0;
            double payment = 0.0;             
            
            // calculate price with customer discount
            if (newSale.getCustomer() != null)
            {
                totalSalePriceWithDiscount = totalSalePrice * (1 - newSale.getCustomer().getDiscount());         
            }
            else
            {   
                // sets totalSalePriceWithDiscount equal to totalSalePrice if there is no customer
                totalSalePriceWithDiscount = totalSalePrice;
            }
                
            // checks if payment is equal to the totalSaleprice
            while(!checkPayment)
            {   
                
                System.out.println("Samlet pris: " + totalSalePrice);
                if (newSale.getCustomer() != null)
                {
                    System.out.println("Kundens rabat: " + (newSale.getCustomer().getDiscount() * 100) + "%");
                    System.out.println("");
                }
                else
                {
                    System.out.println("Ingen rabat for købet");
                }
                
                if (newSale.getCustomer() != null)
                {
                    System.out.println("Totale pris:   " + totalSalePriceWithDiscount);
                }
                
                System.out.println("Gennemfør betaling ved at indtaste salgets endelig pris (" + totalSalePriceWithDiscount + ")");
                System.out.println("Du skal bruge et komma i betalingen");
                
                payment = inputDouble();                    
                                
                if (payment == totalSalePriceWithDiscount)
                {   
                    checkPayment = true;
                    addPayment(payment);
                }                
            }
        }
        else
        {   
            // error message, if there isnt assigned an employee to the sale
            System.out.println("Der er ikke tilknyttet en ansat til salget.");
            System.out.println("Det skal der være for at kunne gennemføre salget");
        }
    }
    
    /**
     * adds payment for the finished sale, then empties the sale newSale
     */
    public void addPayment(double payment)
    {   
        newSale.printInvoice();
        System.out.println("");
        System.out.println("Betaling blev gennemført");
        System.out.println("Det færdige salg blev overført til databasen.");
        Sale completeSale = newSale;
        saleContainer.addSale(completeSale);
                
        // newSale is ready for a new sale
        completeSale.removeInstance();
        newSale.removeInstance();
    } 
    
    /**
     * Finds a sale in the ArrayList sales 
     * @param  invoiceNumber the nummer for the sale to search for
     */
    public void findSale(int searchInvoiceNumber)
    {   
        Sale foundSale = saleContainer.findSale(searchInvoiceNumber);
        
        if (foundSale != null)
        {   
            System.out.println("Sale med invoice nummer " + searchInvoiceNumber + "blev fundet");
            listSale(searchInvoiceNumber);
        }
    }
    
    /**
     * Deletes a sale in the ArrayList sales
     * @param  invoiceNumber the nummer for the sale to delete
     */
    public void deleteSale(int searchInvoiceNumber)
    {   
        saleContainer.deleteSale(searchInvoiceNumber);
    }
    
     /**
     * prints a wanted sale in the ArrayList sales as output
     * @param  searchInvoiceNumber 
     */
    public void listSale(int searchInvoiceNumber)
    {
        saleContainer.listSale(searchInvoiceNumber);
    }
    
    /**
     *  lists all sales in the ArrayList sales 
     */
    public void listAllSales()
    {   
        saleContainer.listAllSales();
    }    
    
    public Sale getNewSale()
    {
    	return newSale;
    }
    
    public void setNewSale()
    {
    	this.newSale = null;
    }
}